#include "application.h"


extern INT8S video_record_sts;

#if C_CYCLIC_VIDEO_RECORD == CUSTOM_ON
extern INT8U cyclic_record_timerid;
extern STOR_SERV_FILEINFO next_file_info;
#endif

#if C_MOTION_DETECTION == CUSTOM_ON
extern INT8U motion_detect_timerid;
#endif

extern INT8S g_lock_current_file_flag ;
extern INT8U g_cycle_record_continuing_flag ;
extern Overlap_Record_Function Record;

#if GP_UI == Touch_UI
INT8S ap_video_record_init_TP(void)
{
	//INT8U	night_mode;
	INT8U	temp;

	OSQPost(DisplayTaskQ, (void *) MSG_DISPLAY_TASK_EFFECT_INIT);

	ap_display_show_date_time(SHOW_TIME_RECORD);

	if(ap_state_config_voice_record_switch_get())
	{
		ap_state_handling_icon_show_cmd(ICON_MIC_ON, 0, 0);
	}
	else
	{
		ap_state_handling_icon_show_cmd(ICON_MIC_OFF, 0, 0);
	}

	ap_state_handling_icon_show_cmd(ICON_REC_STOP, 0, 0);
	ap_state_handling_icon_show_cmd(ICON_HOME, 0, 0);
	msgQSend(PeripheralTaskQ, MSG_PERIPHERAL_HOME_REC_MIC_ICON_SHOW, NULL, 0, MSG_PRI_NORMAL);

	ap_video_record_resolution_display();


	temp = ap_state_config_park_mode_G_sensor_get();
	if(temp)
	{
		ap_video_record_show_park_mode();
	}

	//temp = ap_state_config_ev_get();
	//drv_l2_cdsp_set_ev_val(temp);
	//0:+2, 1:+5/3, 2:+4/3, 3:+1.0, 4:+2/3, 5:+1/3, 6:+0.0, 7:-1/3, 8:-2/3, 9:-1.0, 10:-4/3, 11:-5/3, 12:-2.0

	temp = ap_state_config_light_freq_get();
	drv_l2_user_sensor_frequence(temp);

	#if C_BATTERY_DETECT
	ap_state_handling_current_bat_lvl_show();
	ap_state_handling_current_charge_icon_show();
	#endif // #if C_BATTERY_DETECT

	#if C_CYCLIC_VIDEO_RECORD
	cyclic_record_timerid = 0xFF;
	next_file_info.file_handle = -1;

	#if GPS_TXT
	next_file_info.txt_handle = -1;
	#endif // #if GPS_TXT
	#endif // C_CYCLIC_VIDEO_RECORD

	g_lock_current_file_flag = 0;
	g_cycle_record_continuing_flag = 0;

	ap_video_record_sts_set(0);
	if(ap_state_handling_storage_id_get() == NO_STORAGE)
	{
		ap_video_record_sts_set(VIDEO_RECORD_UNMOUNT);
		video_calculate_left_recording_time_disable();

		#if C_MOTION_DETECTION
		if(ap_state_config_md_get())
		{
			Record.ap_video_record_md_icon_clear_all_ptr();
			ap_video_record_md_disable();
			ap_state_config_md_set(0);
		}
		#endif // #if C_MOTION_DETECTION

		return STATUS_FAIL;
	}
	else
	{
		ap_state_handling_icon_show_cmd(ICON_VIDEO_RECORD, ICON_SD_CARD, 0);
		ap_video_record_sts_set(~VIDEO_RECORD_UNMOUNT);
		video_calculate_left_recording_time_enable();

		#if C_MOTION_DETECTION
		if(ap_state_config_md_get())
		{
			//ap_state_handling_icon_show_cmd(ICON_MD_STS_0, 0, 0);
			msgQSend(PeripheralTaskQ, MSG_PERIPHERAL_TASK_MOTION_DETECT_START, NULL, 0, MSG_PRI_NORMAL);
			ap_video_record_sts_set(VIDEO_RECORD_MD);
		}
		#endif // #if C_MOTION_DETECTION


		return STATUS_OK;
	}
}

void ap_video_record_exit_TP(void)
{
    ap_state_handling_icon_clear_cmd(ICON_MIC_ON, 0, 0);
	ap_state_handling_icon_clear_cmd(ICON_MIC_OFF, 0, 0);
	ap_state_handling_icon_clear_cmd(ICON_REC_STOP, 0, 0);
	ap_state_handling_icon_clear_cmd(ICON_REC_TOUCH, 0, 0);
	ap_state_handling_icon_clear_cmd(ICON_HOME, 0, 0);
	ap_state_handling_icon_clear_cmd(ICON_VIDEO_RECORD, 0, 0);
	ap_video_record_clear_resolution_str();
	ap_video_record_clear_park_mode();

	ap_display_show_date_time(SHOW_TIME_NONE);
}

#if C_MOTION_DETECTION == CUSTOM_ON
void ap_video_record_md_icon_clear_all_TP(void)
{
}

void ap_video_record_md_tick_TP(INT8U *md_tick)
{
	++ *md_tick;
	if(*md_tick > MOTION_DETECT_STOP_TIME)
	{
		*md_tick = 0;
		if(motion_detect_timerid != 0xFF)
		{
			sys_kill_timer(motion_detect_timerid);
			motion_detect_timerid = 0xFF;
		}

		if(ap_video_record_sts_get() & VIDEO_RECORD_MD)
		{
			if(video_record_sts & VIDEO_RECORD_BUSY)
			{
				ap_video_record_func_key_active();
			}

			//ap_state_handling_icon_clear_cmd(ICON_MOTION_DETECT_START, 0, 0);
			DBG_PRINT("Motion detect md_tick Show icon!\r\n");
		}
	}
}
#endif //#if C_MOTION_DETECTION

#endif   //GP_UI ==Touch_UI
